// pages/demand/check-success.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        selectedList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData(options)
        this.loadData()
    },
    /**
     * 多项选择
     */
    checkboxChange: function(e) {
        let list = this.data.list
        let selectedList = []
        let values = e.detail.value
        for (let i = 0; i < list.length; ++i) {
            list[i].checked = false;
            for (let j = 0; j < values.length; ++j) {
                if (list[i].code == values[j]) {
                    list[i].checked = true;
                    selectedList.push(list[i])
                    break;
                }
            }
        }
        this.setData({
            list,
            selectedList
        });
    },
    loadData() {
        wx.showLoading({
            title: '加载中...',
        })
        app.http.post('rest/ticket/queryById.do', {
            data: this.data.id
        }).then(res => {
            this.setData({
                status:res.obj.status,
                list: res.obj.violationLst.map(o => {
                    o.checked = false
                    return o
                })
            })
            wx.hideLoading()
			wx.stopPullDownRefresh()
        }).catch(err => {
            wx.hideLoading()
            app.wxToast.error(err.data.msg)
        })
    },
    submit() {
        wx.showLoading({
            title: '请稍等...',
        })
        let codeArr = this.data.selectedList.map(o => o.code)
        app.http.post('rest/ticket/handleSuccess.do', {
            data: {
                id: this.data.id,
                violationCodes: codeArr.join(',')
            }
        }).then(res => {
			wx.navigateBack({})
        })
    },
	onPullDownRefresh(){
		this.loadData()
	}
})